package cn.edu.xmu.orderpayment.payment.controller;

import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.orderpayment.payment.constants.Constants;
import cn.edu.xmu.orderpayment.payment.microservice.alipayvo.NotifyBody;
import cn.edu.xmu.orderpayment.payment.microservice.wechatpayvo.WeChatPayPaymentNotifyRetVo;
import cn.edu.xmu.orderpayment.payment.microservice.wechatpayvo.WeChatPayRefundNotifyRetVo;
import cn.edu.xmu.orderpayment.payment.microservice.wechatpayvo.WeChatPayTransactionRetVo;
import cn.edu.xmu.orderpayment.payment.model.bo.ErrorAccount;
import cn.edu.xmu.orderpayment.payment.model.bo.Payment;
import cn.edu.xmu.orderpayment.payment.model.bo.Refund;
import cn.edu.xmu.orderpayment.payment.model.vo.*;
import cn.edu.xmu.orderpayment.payment.service.PayService;
import cn.edu.xmu.privilegegateway.annotation.aop.Audit;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginName;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginUser;
import cn.edu.xmu.privilegegateway.annotation.util.InternalReturnObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;

import java.time.LocalDateTime;
import java.time.ZonedDateTime;

import static cn.edu.xmu.oomall.core.util.Common.*;
import static cn.edu.xmu.privilegegateway.annotation.util.Common.cloneVo;

/**
 * @author yunlong
 * @date 2021-12-02
 */
@RestController
@RequestMapping(value = "/", produces = "application/json;charset=UTF-8")
public class PayController {
    private final Logger logger = LoggerFactory.getLogger(PayController.class);

    @Autowired
    private HttpServletResponse httpServletResponse;

    @Autowired
    private PayService payService;

    @ApiOperation(value = "获得支付渠道的所有状态")
    @GetMapping("/paypatterns/states")
    public Object getPaymentPatternState(){
        ReturnObject res = payService.getPaymentPatternState();
        return decorateReturnObject(res);
    }

    @ApiOperation(value = "获得当前有效的支付渠道")
    @GetMapping("/paypatterns")
    public Object getValidPaymentPattern(){
        // 从token获得id 传入service
        ReturnObject res = payService.getValidPaymentPattern();
        return decorateReturnObject(res);
    }

    @ApiOperation(value = "获得支付单的所有状态")
    @GetMapping("/payment/states")
    public Object getPaymentState(){
        ReturnObject res = payService.getPaymentState();
        return decorateReturnObject(res);
    }
    @ApiOperation(value = "获得支付单的所有状态")
    @GetMapping("/refund/states")
    public Object getrefundState(){
        ReturnObject res = payService.getRefundState();
        return decorateReturnObject(res);
    }

    @ApiOperation(value = "获得所有的支付渠道")
    @GetMapping("/shops/{shopId}/paypatterns")
    public Object getPayPatterns(@PathVariable Long shopId){
        if (0!=shopId){
            ReturnObject ret = new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE);//不是0，说明不是管理员，返回505错误
            return decorateReturnObject(ret);
        }
        ReturnObject res = payService.getPayPatterns();
        return decorateReturnObject(res);
    }

    @ApiOperation(value = "顾客支付已建立的支付单")
    @Audit
    @PutMapping("payments/{pid}/pay")
    public  Object payExist(@LoginUser Long userId,@LoginName String userName,@PathVariable Long pid,@Validated @RequestBody PatternInfoVo patternInfo,BindingResult bindingResult){
        Object returnObject = processFieldErrors(bindingResult, httpServletResponse);
        if (null != returnObject) {
            return returnObject;
        }

        ReturnObject res=payService.payExist(pid,patternInfo.getPatternId(),userId,userName);
        if(res.getCode()!=ReturnNo.OK){
            return decorateReturnObject(res);
        }
        return decorateReturnObject(res);

    }


    @ApiOperation(value = "顾客支付")
    @Audit
    @PostMapping("payments")
    public Object pay(@LoginUser Long userId, @LoginName String loginName, @Validated @RequestBody NewPaymentVo newPaymentVo,BindingResult bindingResult){
        Object returnObject = processFieldErrors(bindingResult, httpServletResponse);
        if (null != returnObject) {
            return returnObject;
        }

        if(newPaymentVo.getEndTime().isBefore(newPaymentVo.getBeginTime())){
            return decorateReturnObject(new ReturnObject(ReturnNo.LATE_BEGINTIME));
        }

        ReturnObject res=payService.createPayment(newPaymentVo,userId,loginName);
        if(res.getCode()!=ReturnNo.OK){
            return decorateReturnObject(res);
        }
        httpServletResponse.setStatus(HttpServletResponse.SC_CREATED);
        return decorateReturnObject(res);

    }

    /**
     * 内部API 支付
     * @param userId
     * @param loginName
     * @param newPaymentVo
     * @param bindingResult
     * @return
     */
    @Audit
    @ApiOperation(value = "支付")
    @PostMapping("internal/payments")
    public InternalReturnObject internalPay(@LoginUser Long userId, @LoginName String loginName, @Validated @RequestBody NewPaymentVo newPaymentVo,BindingResult bindingResult){
        Object returnObject = processFieldErrors(bindingResult, httpServletResponse);
        if (null != returnObject) {
            return new InternalReturnObject(ReturnNo.FIELD_NOTVALID.getCode(),ReturnNo.FIELD_NOTVALID.getMessage());
        }

        if(newPaymentVo.getEndTime().isBefore(newPaymentVo.getBeginTime())){
            return new InternalReturnObject(ReturnNo.LATE_BEGINTIME.getCode(),ReturnNo.LATE_BEGINTIME.getMessage());
        }

        ReturnObject res=payService.createPayment(newPaymentVo,userId,loginName);
        InternalReturnObject ret = new InternalReturnObject();
        if(res.getData()!=null)
            ret.setData(res.getData());
        ret.setErrno(res.getCode().getCode());
        ret.setErrmsg(res.getErrmsg());
        if(res.getCode()!=ReturnNo.OK){
            return ret;
        }
        httpServletResponse.setStatus(HttpServletResponse.SC_CREATED);
        return ret;

    }



    @ApiOperation(value = "平台管理员查询支付信息")
    @GetMapping("/shops/{shopId}/payment")
    public Object getPaymentInfo(
            @PathVariable Long shopId,
            @RequestParam(required = false) String documentId,
            @RequestParam(required = false) Byte state,
            @RequestParam(value = "beginTime", required = false) @DateTimeFormat(pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSXXX") ZonedDateTime beginTime,
            @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSXXX") ZonedDateTime endTime,
            @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        if (0!=shopId){
            ReturnObject ret = new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE);//不是0，说明不是管理员，返回505错误
            return decorateReturnObject(ret);
        }
        ReturnObject ret = payService.getPaymentInfo(documentId,state,beginTime,endTime,page,pageSize);
        return decorateReturnObject(Common.getPageRetVo(ret, SimplePaymentRetVo.class));

    }


    @ApiOperation(value = "平台管理员查询支付信息详情")
    @GetMapping("/shops/{shopId}/payment/{id}")
    public Object getPaymentDetailInfo(@PathVariable Long shopId,@PathVariable Long id){
        if (0!=shopId){
            ReturnObject ret = new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE);//不是0，说明不是管理员，返回505错误
            return decorateReturnObject(ret);
        }
        ReturnObject res = payService.getPaymentDetailInfo(id);

        return decorateReturnObject(res);
    }

    /**
     *
     * @param shopId
     * @param id
     * @param loginId
     * @param loginName
     * @param modifyInfoVo
     * @param bindingResult
     * @return
     * @author wang xusheng
     */
    @ApiOperation(value = "平台管理员修改支付信息")
    @Audit
    @PutMapping("/shops/{shopId}/payment/{id}")
    public Object modifyPaymentInfo(@PathVariable Long shopId, @PathVariable Long id,
                                    @LoginUser Long loginId, @LoginName String loginName,
                                    @Validated @RequestBody ModifyInfoVo modifyInfoVo,
                                    BindingResult bindingResult){
        if (0!=shopId){
            ReturnObject ret = new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE,"非管理员不能操作");//不是0，说明不是管理员，返回505错误
            return decorateReturnObject(ret);
        }
        Object returnObject = processFieldErrors(bindingResult, httpServletResponse);
        if (null != returnObject) {
            return returnObject;
        }
        Payment payment=cloneVo(modifyInfoVo,Payment.class);
        payment.setId(id);
        ReturnObject res = payService.modifyPaymentInfo(payment,loginId,loginName);
        return decorateReturnObject(getRetVo(res, PaymentRetVo.class));
    }

    @ApiOperation(value = "平台管理员查询退款信息")
    @GetMapping("/shops/{shopId}/refund")
    public Object getRefundInfo(
            @PathVariable Long shopId,
            @RequestParam(required = false) String documentId,
            @RequestParam(required = false) Byte state,

            @RequestParam(value = "beginTime", required = false) @DateTimeFormat(pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSXXX") ZonedDateTime beginTime,
            @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSXXX") ZonedDateTime endTime,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize

            ){
        if (0!=shopId){
            ReturnObject ret = new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE);//不是0，说明不是管理员，返回505错误
            return decorateReturnObject(ret);
        }
        ReturnObject res = payService.getRefundInfo(documentId,state,beginTime,endTime,page,pageSize);
        return decorateReturnObject(Common.getPageRetVo(res, SimpleRefundRetVo.class));
    }

    @ApiOperation(value = "平台管理员查询退款信息详情")
    @GetMapping("/shops/{shopId}/refund/{id}")
    public Object getRefundDetailInfo(@PathVariable Long shopId, @PathVariable Long id){
        if (0!=shopId){
            ReturnObject ret = new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE);//不是0，说明不是管理员，返回505错误
            return decorateReturnObject(ret);
        }
        ReturnObject res = payService.getRefundDetailInfo(id);
        if(res.getData()!=null){
            Refund refund=(Refund)res.getData();
            RefundRetVo refundRetVo=cloneVo(refund, RefundRetVo.class);
            refundRetVo.setAdjust(new SimpleAdminUserVo(refund.getAdjustId(), refund.getAdjustName()));
            refundRetVo.setCreator(new SimpleAdminUserVo(refund.getCreatorId(), refund.getCreatorName()));
            refundRetVo.setModifier(new SimpleAdminUserVo(refund.getModifierId(), refund.getModifierName()));
            res= new ReturnObject(refundRetVo);
        }
        return decorateReturnObject(res);
    }

    /**
     *
     * @param shopId
     * @param id
     * @param loginId
     * @param loginName
     * @param modifyInfoVo
     * @param bindingResult
     * @return
     * @author wang xusheng
     */
    @ApiOperation(value = "平台管理员修改退款信息")
    @Audit
    @PutMapping("/shops/{shopId}/refund/{id}")
    public Object modifyRefundInfo(@PathVariable Long shopId, @PathVariable Long id,
                                   @LoginUser Long loginId, @LoginName String loginName,
                                   @Validated @RequestBody ModifyInfoVo modifyInfoVo,
                                   BindingResult bindingResult){
        if (0!=shopId){
            ReturnObject ret = new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE,"非管理员不能操作");//不是0，说明不是管理员，返回505错误
            return decorateReturnObject(ret);
        }
        Object returnObject = processFieldErrors(bindingResult, httpServletResponse);
        if (null != returnObject) {
            return returnObject;
        }
        Refund refund=cloneVo(modifyInfoVo, Refund.class);
        refund.setId(id);
        ReturnObject res = payService.modifyRefundInfo(refund,loginId,loginName);
        return decorateReturnObject(getRetVo(res, RefundRetVo.class));
    }

    @ApiOperation(value = "平台管理员查询错帐信息")
    @GetMapping("/shops/{shopId}/erroraccounts")
    public Object getErrorAccount(
            @PathVariable Long shopId,
            @RequestParam(required = false) String documentId,
            @RequestParam(required = false) Byte state,

            @RequestParam(value = "beginTime", required = false) @DateTimeFormat(pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSXXX") ZonedDateTime beginTime,
            @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSXXX") ZonedDateTime endTime,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize
    ){
        if (0!=shopId){
            ReturnObject ret = new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE);//不是0，说明不是管理员，返回505错误
            return decorateReturnObject(ret);
        }
        //若参数含有时间，合法性检查
        ReturnObject res = payService.getErrorAccount(documentId,state,beginTime,endTime,page,pageSize);
        return decorateReturnObject(Common.getPageRetVo(res, SimpleErrorAccountRetVo.class));
    }

    @ApiOperation(value = "平台管理员查询错账信息详情")
    @GetMapping("/shops/{shopId}/erroraccounts/{id}")
    public Object getErrorAccountDetailInfo(
            @PathVariable Long shopId,
            @PathVariable Long id
    ){
        if (0!=shopId){
            ReturnObject ret = new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE);//不是0，说明不是管理员，返回505错误
            return decorateReturnObject(ret);
        }
        ReturnObject res = payService.getErrorAccountDetailInfo(id);
        if(res.getData()!=null){
            ErrorAccount errorAccount=(ErrorAccount)res.getData();
            ErrorAccountRetVo errorAccountRetVo=cloneVo(errorAccount, ErrorAccountRetVo.class);
            errorAccountRetVo.setAdjust(new SimpleAdminUserVo(errorAccount.getAdjustId(), errorAccount.getAdjustName()));
            errorAccountRetVo.setCreator(new SimpleAdminUserVo(errorAccount.getCreatorId(), errorAccount.getCreatorName()));
            errorAccountRetVo.setModifier(new SimpleAdminUserVo(errorAccount.getModifierId(), errorAccount.getModifierName()));
            res= new ReturnObject(errorAccountRetVo);
        }
        return decorateReturnObject(res);
    }

    /**
     *
     * @param shopId
     * @param id
     * @param loginId
     * @param loginName
     * @param modifyInfoVo
     * @param bindingResult
     * @return
     * @author wang xusheng
     */
    @ApiOperation(value = "平台管理员修改错账信息")
    @Audit
    @PutMapping("/shops/{shopId}/erroraccounts/{id}")
    public Object modifyErrorAccountInfo(@PathVariable Long shopId, @PathVariable Long id,
                                         @LoginUser Long loginId, @LoginName String loginName,
                                         @Validated @RequestBody ModifyInfoVo modifyInfoVo,
                                         BindingResult bindingResult){
        if (0!=shopId){
            ReturnObject ret = new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE,"非管理员不能操作");//不是0，说明不是管理员，返回505错误
            return decorateReturnObject(ret);
        }
        Object returnObject = processFieldErrors(bindingResult, httpServletResponse);
        if (null != returnObject) {
            return returnObject;
        }
        ErrorAccount errorAccount = cloneVo(modifyInfoVo,ErrorAccount.class);
        errorAccount.setId(id);
        ReturnObject res = payService.modifyErrorAccountInfo(errorAccount,loginId,loginName);
        return decorateReturnObject(getRetVo(res, ErrorAccountRetVo.class));
    }

    /**
     *
     * @param loginId
     * @param loginName
     * @param beginTime
     * @param endTime
     * @return
     * @author wang xusheng
     */
    @ApiOperation(value = "对账")
    @Audit
    @GetMapping("/shops/{id}/reconciliation")
    public Object reconciliation(@PathVariable Long id,@LoginUser Long loginId,@LoginName String loginName,
                                 @RequestParam(value = "beginTime", required = false) @DateTimeFormat(pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSXXX") ZonedDateTime beginTime,
                                 @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSXXX") ZonedDateTime endTime){
        if (0!=id){
            ReturnObject ret = new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE,"非管理员不能操作");//不是0，说明不是管理员，返回505错误
            return decorateReturnObject(ret);
        }
        //参数时间合法性检查
        if(beginTime.isAfter(endTime)){
            ReturnObject ret = new ReturnObject(ReturnNo.LATE_BEGINTIME);
            return decorateReturnObject(ret);
        }
        ReturnObject res = payService.reconciliation(loginId,loginName,beginTime,endTime);
        return decorateReturnObject(res);
    }

    @ApiOperation(value = "微信支付通知API")
    @PostMapping("/wechat/payment/notify")
    public Object wechatPaymentNotify(
            @RequestBody WeChatPayPaymentNotifyRetVo weChatPayPaymentNotifyRetVo){
        logger.info(LocalDateTime.now()+":支付系统收到微信传来的回调结果");

        ServletRequestAttributes servletRequestAttributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Constants.servletRequestAttributes=servletRequestAttributes;

        WeChatPayTransactionRetVo weChatPayTransactionRetVo = weChatPayPaymentNotifyRetVo.getResource().getCiphertext();
        ReturnObject res = payService.wechatPaymentNotify(weChatPayTransactionRetVo);
        return decorateReturnObject(res);
    }

    @ApiOperation(value = "微信退款通知API")
    @PostMapping("/wechat/refund/notify")
    public Object wechatRefundNotify(@RequestBody WeChatPayRefundNotifyRetVo weChatPayRefundNotifyRetVo){
        logger.info(LocalDateTime.now()+":支付系统收到微信传来的退款回调结果");

//        logger.info(weChatPayPaymentNotifyRetVo);
        ServletRequestAttributes servletRequestAttributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Constants.servletRequestAttributes=servletRequestAttributes;
        ReturnObject res = payService.wechatRefundNotify(weChatPayRefundNotifyRetVo);
        return decorateReturnObject(res);
    }

    @ApiOperation(value = "阿里异步通知API")
    @PostMapping("/alipay/notify")
    public Object alipayNotify(@RequestBody NotifyBody notifyBody){
        logger.info("alipay notify");
//        logger.info(weChatPayPaymentNotifyRetVo);
        ServletRequestAttributes servletRequestAttributes= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Constants.servletRequestAttributes=servletRequestAttributes;
        ReturnObject res = payService.alipayNotify(notifyBody);
        return decorateReturnObject(res);
    }

    /**
     *  内部API：订单获得支付
     */
    @GetMapping("internal/document/{documentId}/payment")
    public InternalReturnObject getPaymentByDocumentId(@PathVariable("documentId")String documentId){
        ReturnObject res= payService.getPaymentByDocumentId(documentId);

        InternalReturnObject ret = new InternalReturnObject();
        if(res.getData()!=null)
            ret.setData(res.getData());
        ret.setErrno(res.getCode().getCode());
        ret.setErrmsg(res.getErrmsg());
        return ret;
    }

    /**
     * 内部API 订单获得退款
     * @param documentId
     * @return
     */
    @GetMapping("internal/document/{documentId}/refund")
    public InternalReturnObject getRefundByDocumentId(@PathVariable String documentId){
        ReturnObject res= payService.getRefundByDocumentId(documentId);

        InternalReturnObject ret = new InternalReturnObject();
        if(res.getData()!=null)
            ret.setData(res.getData());
        ret.setErrno(res.getCode().getCode());
        ret.setErrmsg(res.getErrmsg());
        return ret;
    }

    /**
     * 内部API 订单退款
     * @param documentId
     * @param amount
     * @return
     */
    @PostMapping("internal/document/{documentId}/refund")
    public InternalReturnObject refund(@PathVariable String documentId, @RequestParam Long amount, @RequestParam Byte documentType, @RequestParam String descr){
        ReturnObject res=payService.createRefund(documentId,amount,documentType,descr);

        InternalReturnObject ret = new InternalReturnObject();
        if(res.getData()!=null)
            ret.setData(res.getData());
        ret.setErrno(res.getCode().getCode());
        ret.setErrmsg(res.getErrmsg());
        return ret;
    }


    /**
     * 内部API 订单全额退款
     * @param documentId
     * @return
     */
    @PostMapping("internal/document/{documentId}/refund/all")
    public InternalReturnObject refundAll(@PathVariable String documentId,@RequestParam Byte documentType,@RequestParam String descr){
        ReturnObject res=payService.createRefundAll(documentId,documentType,descr);

        InternalReturnObject ret = new InternalReturnObject();
        if(res.getData()!=null)
            ret.setData(res.getData());
        ret.setErrno(res.getCode().getCode());
        ret.setErrmsg(res.getErrmsg());
        return ret;
    }



    @ApiOperation(value = "内部API支付信息")
    @GetMapping("/internal/shops/{shopId}/payment")
    public InternalReturnObject getPaymentByStateAndTime(
            @PathVariable Long shopId,
            @RequestParam(required = false) String documentId,
            @RequestParam(required = false) Byte state,
            @RequestParam(value = "beginTime", required = false) @DateTimeFormat(pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSXXX") ZonedDateTime beginTime,
            @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSXXX") ZonedDateTime endTime,
            @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        if (0!=shopId){
            return new InternalReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE.getCode(),ReturnNo.RESOURCE_ID_OUTSCOPE.getMessage());
        }
        ReturnObject res = payService.getPaymentInfo(documentId,state,beginTime,endTime,page,pageSize);
        res = Common.getPageRetVo(res, SimplePaymentRetVo.class);

        InternalReturnObject ret = new InternalReturnObject();
        if(res.getData()!=null)
            ret.setData(res.getData());
        ret.setErrno(res.getCode().getCode());
        ret.setErrmsg(res.getErrmsg());
        return ret;
    }


    @ApiOperation(value = "内部API查询退款信息")
    @GetMapping("/internal/shops/{shopId}/refund")
    public InternalReturnObject getRefundByStateAndTime(
            @PathVariable Long shopId,
            @RequestParam(required = false) String documentId,
            @RequestParam(required = false) Byte state,

            @RequestParam(value = "beginTime", required = false) @DateTimeFormat(pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSXXX") ZonedDateTime beginTime,
            @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "uuuu-MM-dd'T'HH:mm:ss.SSSXXX") ZonedDateTime endTime,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize

    ){
        if (0!=shopId){
            return new InternalReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE.getCode(),ReturnNo.RESOURCE_ID_OUTSCOPE.getMessage());
        }
        ReturnObject res = payService.getRefundInfo(documentId,state,beginTime,endTime,page,pageSize);
        res = Common.getPageRetVo(res, SimpleRefundRetVo.class);

        InternalReturnObject ret = new InternalReturnObject();
        if(res.getData()!=null)
            ret.setData(res.getData());
        ret.setErrno(res.getCode().getCode());
        ret.setErrmsg(res.getErrmsg());
        return ret;
    }


    /**
     * 修改支付单状态为已清算
     * @param id
     * @return
     */
    @PutMapping("/internal/payment/{id}/liquidated")
    public InternalReturnObject changePaymentLiquidated(@PathVariable("id") Long id){
        ReturnObject res=payService.setLiquidatedPayment(id);

        InternalReturnObject ret = new InternalReturnObject();
        if(res.getData()!=null)
            ret.setData(res.getData());
        ret.setErrno(res.getCode().getCode());
        ret.setErrmsg(res.getErrmsg());
        return ret;
    }

    /**
     * 修改退款单状态为已清算
     * @param id
     * @return
     */
    @PutMapping("/internal/refund/{id}/liquidated")
    public InternalReturnObject changeRefundLiquidated(@PathVariable("id") Long id){
        ReturnObject res=payService.setLiquidatedRefund(id);

        InternalReturnObject ret = new InternalReturnObject();
        if(res.getData()!=null)
            ret.setData(res.getData());
        ret.setErrno(res.getCode().getCode());
        ret.setErrmsg(res.getErrmsg());
        return ret;
    }



}
